<?php
/**
 * Created by PhpStorm.
 * User: jucai002
 * Date: 2019/1/30
 * Time: 10:35
 */

namespace app\modules\admin\controllers;


use app\models\AdminUser;
use yii\base\Module;
use yii\web\Controller;

class LoginController extends Controller
{
    public $enableCsrfValidation = false;
    public $layout = false;
    public function __construct($id, Module $module, array $config = [])
    {
        if(empty($_SESSION)) session_start();
        parent::__construct($id, $module, $config);
    }

    /**
     * @return string
     * 登录入口
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {
        $username = \Yii::$app->request->post('username', false);
        $password = \Yii::$app->request->post('password', false);
        if (empty($username) || empty($password)) return  $this->redirect(\Yii::$app->urlManager->createUrl(['admin/login/index']));
        //查询数据库
        $admin = AdminUser::find()->where(['username' => $username, 'password' => $password])->one();
        if (empty($admin))  return  $this->redirect(\Yii::$app->urlManager->createUrl(['admin/login/index']));
        //存储session
        $data = json_encode([
            'id'=>$admin->id,
            'name'=>$username,
            'grade'=>$admin->grade,
            'ju_1'=>$admin->Jurisdiction_1,
            'ju_2'=>$admin->Jurisdiction_2,
        ]);
        $_SESSION['admin_login'] = $data;
        return  $this->redirect(\Yii::$app->urlManager->createUrl(['admin/index/index']));
    }

}